package com.kang.until;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.kang.pojo.AjaxResult;

/**
 * 登录拦截器
 * 
 * @author zs614
 *
 */
@Repository
public class SystemInterceptor extends HandlerInterceptorAdapter {

	public void afterCompletion(HttpServletRequest arg0,

			HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	/**
	 * 访问权拦截，判断是否登录
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		Integer userId = (Integer) request.getSession().getAttribute("userID");
		if (userId == null) {
			if (RequestUtil.isAjax(request)) {
				AjaxResult result = new AjaxResult();
				result.setResult("noLogin");
				response.getOutputStream().write(result.toJSONString().getBytes());
			} else {
				if (request.getRequestURI().indexOf("bootstrap") > 0)
					return true;
				response.sendRedirect(request.getContextPath() + "/notLogin.do");
			}
			// 用户没有登录
			return false;
		} else {
			// 用户已经登录
			return true;

		}

	}

}